package com.fast.alden.portal.web.controller;

import com.fast.alden.common.entity.dto.LoginParam;
import com.fast.alden.common.entity.dto.LoginResult;
import com.fast.alden.common.entity.dto.VerifyCodeEntity;
import com.fast.alden.portal.entity.dto.RegisterParam;
import com.fast.alden.portal.entity.dto.ResetPasswordParam;
import com.fast.alden.portal.service.AuthPortalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 认证与鉴权控制器
 */
@Tag(name = "AuthController", description = "授权管理")
@RestController
@RequestMapping("/auth")
public class AuthPortalController {
    @Resource
    private AuthPortalService authService;

    @Operation(summary = "获取验证码")
    @GetMapping("/verify-code")
    public VerifyCodeEntity generateVerifyCode() throws IOException {
        return authService.generateVerifyCode();
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public LoginResult login(@RequestBody @Validated LoginParam param) {
        return authService.login(param);
    }

    @Operation(summary = "注销登录")
    @PostMapping("/logout")
    public void logout() {
        authService.logout();
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public void register(@RequestBody @Validated RegisterParam param) {
        authService.register(param);
    }

    @Operation(summary = "忘记密码校验用户名是否存在")
    @GetMapping("/forget-password/check/username/{username}")
    @ResponseBody
    public boolean checkUsernameBeforeResetPassword(@PathVariable("username") String username) {
        return authService.checkUsernameBeforeResetPassword(username);
    }

    @Operation(summary = "忘记密码校验")
    @PostMapping("/forget-password/check")
    @ResponseBody
    public boolean checkMainInfo(@RequestBody ResetPasswordParam param) {
        return authService.checkMainInfo(param);
    }

    @Operation(summary = "忘记密码重置")
    @PutMapping("/forget-password/reset")
    public void resetPassword(@RequestBody ResetPasswordParam param) {
        authService.resetPassword(param);
    }

    @Operation(summary = "注册时用户名校验")
    @GetMapping("/check/username/{username}")
    public boolean checkUsername(@PathVariable("username") String username) {
        return authService.checkUsername(username);
    }

    @Operation(summary = "注册时手机号校验")
    @GetMapping("/check/phone/{phone}")
    public boolean checkPhone(@PathVariable("phone") String phone) {
        return authService.checkPhone(phone);
    }

    @Operation(summary = "注册时邮箱校验")
    @GetMapping("/check/email/{email}")
    public boolean checkEmail(@PathVariable("email") String email) {
        return authService.checkEmail(email);
    }
}
